package xdu.lz.stage5_jucUtils.chapter3_exchanger;

import java.util.concurrent.Exchanger;

/**
 * Created at 2019/10/19 0019 上午 11:03
 */
public class Exchanger1 {
    public static void main(String[] args) {

        Exchanger<String> exchanger = new Exchanger<>();
        /**
         * 注意：该API只适合成对交换数据 ，不适合两个以上一起交换
         */
        new Thread(()->{
            try {
                System.out.println("T1 start");
                //调用该API后阻塞住，直到检测到其他线程达到exchange point
                String res = exchanger.exchange("the data from T1");
                System.out.println(Thread.currentThread().getName()+" receive the data:"+res);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"T1").start();

        new Thread(()->{
            try {
                System.out.println("T2 start");
                //调用该API后阻塞住，直到检测到其他线程达到exchange point
                String res = exchanger.exchange("the data from T2");
                System.out.println(Thread.currentThread().getName()+" receive the data:"+res);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"T2").start();
    }
}
